//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    userInfo: {},
    category: {},
    keywords: {},
    searchData: [],
    searchDataLength: 0,
    imgurl: app.urlimg,
    keyword_list: {},
    //猜你喜欢
    likegoods: [],
  },
  onLoad: function (o) {
    var that = this;
    if ('' != wx.getStorageSync('search')) {
      this.setData({
        searchData: wx.getStorageSync('search')
      });
      this.setData({
        searchDataLength: 1,
      });
      console.log(this.data.searchDataLength);
    }
    // wx.request({
    //   url: app.url + 'api_goods/category_list',
    //   data: {
    //   },
    //   header: {
    //     'content-type': 'application/json',
    //     'token': wx.getStorageSync('token_key')
    //   }, success: function (json) {
    //     that.setData({
    //       category: json.data.result
    //     })
    //   }
    // });
    wx.request({
      url: app.url + 'api_search/keyword_list',
      data: {
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      }, success: function (json) {
        // console.log(json);
        that.setData({
          keywords: json.data.result
        })
      }
    })

    //猜你喜欢
    wx.request({
      url: app.url + 'api_footprint/footprint_group_list',
      data: {
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      },
      success: function (json) {
        that.setData({
          likegoods: json.data.result,
        })
      }
    })
  },
  // 猜你喜欢列表
  click_goods: function (e) {
    wx.navigateTo({
      url: '/pages/index/detail/index?id=' + e.currentTarget.id,
    })
  },
  search: function (e) {
    var that = this;
    var value = e.detail.value.input;
    app.app_load(load_fn);

    function load_fn() {
      //获取formId用于群发
      let formId = e.detail.formId;
      wx.request({
        url: app.url + '?controller=api_search&action=formid',
        data: {
          form_id: formId,
          updated_at: new Date().getTime()
        },
        header: {
          'content-type': 'application/json', // 默认值
          'token': wx.getStorageSync("token_key")
        },
        success: function (res) {
          console.log(res);
        }
      })

      if (value == '') {
        wx.showModal({
          title: '提示',
          content: '请输入关键词'
        })
      } else {
        that.setHisory(value);
        that.opendGoodsList(value);
      }
    }
  },
  onSearch: function (e) {
    var value = e.target.id;
    var that = this;
    app.app_load(load_fn);

    function load_fn() {
      that.setHisory(value);
      that.opendGoodsList(value);
    }
  },
  onKeyword: function (e) {
    var value = e.target.id;
    var that = this;
    app.app_load(load_fn);

    function load_fn() {
      that.opendGoodsList(value);
    }
  },
  // 响应分类单击
  categoryTAP: function (e) {
    var fid = e.currentTarget.id;
    var name = e.currentTarget.dataset.name;
    wx.redirectTo({
      url: '/pages/goods/index?category_id=' + fid + '&name=' + name,
    })
  },
  opendGoodsList: function (value) {
    wx.redirectTo({
      url: '/pages/goods/index?key=' + value
    })
  },
  setHisory: function (value) {
    var searchData = this.data.searchData;
    if (searchData.length >= 10) {
      searchData.pop();
    }
    searchData.unshift(value);
    searchData = this.unique(searchData);
    wx.setStorageSync('search', searchData)
  },
  unique: function (arr) {
    var result = [], hash = {};
    for (var i = 0, elem; (elem = arr[i]) != null; i++) {
      if (!hash[elem]) {
        result.push(elem);
        hash[elem] = true;
      }
    }
    return result;
  },
  clearSearchData: function (e) {
    wx.removeStorageSync('search');
    this.setData({
      searchDataLength: 0,
    });
    this.setData({
      searchData: [],
    });
  }
})

